/**
 * 留言管理
 */
var editor;
var editor2;//回复
$(function(){

	$('#batch_do').click(function(){
		var ac = $('#batch_select').val();
		var msg = (ac == 'delete')?'删除':' 不知干嘛';
		var sel = [];
		
		$('input[name="chk"]').each(function(i){
			if($(this).attr('checked')){
				sel.push($(this).val());
			}
		});
		if(sel.length>0){
			if(ac=='delete'){
				$h.confirm('真的要'+msg, function(m){
					if(m=='ok'){
						$h.load('/sysadmin/guestbook/'+ac+'/'+sel.join('-'),'', function(d){
							for(var i=0; i<sel.length; i++){
								$('#tr_'+sel[i]).remove();
							}
						});
					}
				});
			}
			
		}else{
			$h.alert('木有选择啊，亲');
		}
		return ;
	});
	
	$('a[href="#tab2"]').click(function(){
		$('#frmadd input').each(function(i){
			if($(this).attr('type')=='text' || $(this).attr('type')=='hidden'){
				$(this).val('');
			}
		});
		if(!editor){
			editor = KindEditor.create('textarea[name="content"]');			
		}else{
			editor.html('');
		}
		if(!editor2){
			editor2 = KindEditor.create('textarea[name="reply"]');			
		}else{
			editor2.html('');
		}
		
		$('#sbm').val('添加');
	});
	$('#sbm').click(function(){
		if($('#title').val()!=''){
			$('#content').val(editor.html());
			$('#reply').val(editor2.html());
			var p = $('#frmadd').serialize();
			$h.load('/sysadmin/guestbook/add', p, function(d){
				if(d && d>0){
					location.href=location;
				}else{
					$h.alert('添加失败');return;
				}
			});
		}else{
			$h.alert('烦请填上标题');
			return false;
		}
		return;
	});
});
$h.include([
            '/resources/scripts/kindeditor/kindeditor-min.js',
            '/resources/scripts/kindeditor/lang/zh_CN.js'
            ]);

function del(id){
	$h.confirm('确认删除', function(msg){		
		if(msg == 'ok'){
			$h.load('/sysadmin/guestbook/delete/'+id,'', function(d){
				$('#tr_'+id).remove();
				
			});
			return true;
		}else{
			return false;
		}		
		
	});
	
}
function edit(id){
	$('a[href="#tab2"]').click();
	$h.load('/sysadmin/guestbook/add/'+id, '', function(d){
		log.debug('---请求的数据--');
		log.debug(d);
		if(d && d.length>0){
			var p=d[0];
			$h.fillFormInput_p('frmadd', p);
			editor.insertHtml(p.content);
			if(p.reply){
				editor2.insertHtml(p.reply);
			}
			$('#sbm').val('编辑');
		}
	})
}